package com.lc.demo.controller;

import com.lc.demo.bean.User;
import com.lc.demo.result.ResponseMessage;
import com.lc.demo.service.UserService;
import com.lc.demo.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @Author: sakila
 * @Date: 2022/03/06/12:13
 * @Description:
 */

@RestController
@RequestMapping("/api/auth")
@CrossOrigin
public class AuthController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseMessage login(@RequestBody User user) {
        //登陆验证
        User login = userService.login(user);
        if (login!=null) {
            String token = JwtUtil.createJWT(UUID.randomUUID().toString(), user.getEmail(), null);
            return  ResponseMessage.successWithData(token);
        } else {
            return  ResponseMessage.errorWithMessage("用户名或密码错误");
        }
    }

    /***
     * 新增数据
     * @param user
     * @return
     */
    @PostMapping("/register")
    public ResponseMessage add(@RequestBody User user) {
        userService.add(user);
        return  ResponseMessage.successWithMessage("新增成功");
    }


}
